package com.system.controller;

import com.system.exception.CustomException;
import com.system.entity.*;
import com.system.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/student")
public class StudentController {

    @Resource
    private CourseService courseService;

    @Resource
    private StudentService studentService;

    @Resource
    private SelectedCourseService selectedCourseService;

    @Resource
    private CollegeService collegeService;

    @Resource
    private EvaluateService evaluateService;

    @Resource
    private TeacherService teacherService;

    @RequestMapping(value = "/showCourse")
    public String stuCourseShow(Model model, Integer page) throws Exception {

        List<CourseCustom> list = null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        //设置总页数
        pagingVO.setTotalCount(courseService.getCountCouse());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = courseService.findByPaging(1);
        } else {
            pagingVO.setToPageNo(page);
            list = courseService.findByPaging(page);
        }

        model.addAttribute("courseList", list);
        model.addAttribute("pagingVO", pagingVO);

        return "student/showCourse";
    }

    //搜索课程
    @RequestMapping(value = "/selectCourse", method = {RequestMethod.POST})
    private String selectCourse(String findByName, Model model) throws Exception {

        try {
            List<CourseCustom> list = courseService.findByName(findByName);

            model.addAttribute("courseList", list);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "student/showCourse";
    }

    // 选课操作
    @RequestMapping(value = "/stuSelectedCourse")
    public String stuSelectedCourse(int id) throws Exception {
        //获取当前用户名
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();

        SelectedCourseCustom selectedCourseCustom = new SelectedCourseCustom();
        selectedCourseCustom.setCourseid(id);
        selectedCourseCustom.setStudentid(Integer.parseInt(username));

        SelectedCourseCustom s = selectedCourseService.findOne(selectedCourseCustom);

        if (s == null) {
            selectedCourseService.save(selectedCourseCustom);
        } else {
            throw new CustomException("该门课程你已经选了，不能再选");
        }

        return "redirect:/student/selectedCourse";
    }

    // 退课操作
    @RequestMapping(value = "/outCourse")
    public String outCourse(int id) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();

        SelectedCourseCustom selectedCourseCustom = new SelectedCourseCustom();
        selectedCourseCustom.setCourseid(id);
        selectedCourseCustom.setStudentid(Integer.parseInt(username));

        selectedCourseService.remove(selectedCourseCustom);

        return "redirect:/student/selectedCourse";
    }

    // 已选课程
    @RequestMapping(value = "/selectedCourse")
    public String selectedCourse(Model model) throws Exception {
        //获取当前用户名
        Subject subject = SecurityUtils.getSubject();
        StudentCustom studentCustom = studentService.findStudentAndSelectCourseListByName((String) subject.getPrincipal());

        List<SelectedCourseCustom> list = studentCustom.getSelectedCourseList();

        model.addAttribute("selectedCourseList", list);

        return "student/selectCourse";
    }

    // 已修课程
    @RequestMapping(value = "/overCourse")
    public String overCourse(Model model) throws Exception {

        //获取当前用户名
        Subject subject = SecurityUtils.getSubject();
        StudentCustom studentCustom = studentService.findStudentAndSelectCourseListByName((String) subject.getPrincipal());

        List<SelectedCourseCustom> list = studentCustom.getSelectedCourseList();

        model.addAttribute("selectedCourseList", list);

        return "student/overCourse";
    }

    @RequestMapping(value = "/evaluate")
    public String evaluate(Model model, int id){
        try {
            HashMap<String, Object> map = new HashMap<String, Object>();
            Subject subject = SecurityUtils.getSubject();
            String username = (String) subject.getPrincipal();
            System.out.println("username: " + username);

            // 获取用户id
//            Student student = studentService.selectByName(username);
//            Integer userid = student.getUserid();
            // 获取课程信息
            Course course = courseService.selectById(id);
            Integer teacherID = course.getTeacherid();
            TeacherCustom byId = teacherService.findById(teacherID);

            //拼接
            map.put("courseID", id);
            map.put("courseName", course.getCoursename());
            map.put("teacherName", byId.getUsername());
            map.put("teacherID", teacherID);
            map.put("studentID", username);

            model.addAttribute("evaluateMap", map);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "student/editEvaluate";
    }

    //修改密码
    @RequestMapping(value = "/passwordRest")
    public String passwordRest() throws Exception {
        return "student/passwordRest";
    }

    @RequestMapping(value = "/myShow")
    public String myShow(Model model) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        StudentCustom student = studentService.findById(Integer.parseInt(username));
        HashMap<String, Object> stringObjectHashMap = new HashMap<String, Object>();
        String collegeName = collegeService.selectCollegeName(student.getCollegeid());
        stringObjectHashMap.put("userid", student.getUserid());
        stringObjectHashMap.put("username", student.getUsername());
        stringObjectHashMap.put("sex", student.getSex());
        stringObjectHashMap.put("birthyear", student.getBirthyear());
        stringObjectHashMap.put("grade", student.getGrade());
        stringObjectHashMap.put("collegeName", collegeName);
        model.addAttribute("myShow", stringObjectHashMap);
        return "student/myShow";
    }

    @RequestMapping(value = "/evaluate/add", method = RequestMethod.POST)
    public String addEvaluate(Evaluate  evaluate){

        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();

        evaluate.setStudentID(Integer.parseInt(username));

        evaluateService.add(evaluate);
        selectedCourseService.updateIsOK(evaluate.getCourseID());

        //重定向
        return "redirect:/student/overCourse";
    }



}
